package com.laicunba.event.listener;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.constant.FinancingTargetConstant;
import com.laicunba.event.FriendFirstBuyEvent;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.ActivityService;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.accounting.RewardBidService;
import com.laicunba.service.activity.ActivityDataQueryService;
import com.laicunba.service.activity.dataqueryimpl.FriendInfoActivityDataQueryServiceImpl;

//@Component
public class NewYearInviteListener implements ApplicationListener<FriendFirstBuyEvent> {
	private static final Logger LOG = Logger.getLogger(NewYearInviteListener.class);

	@Autowired
	private RegistryService registryService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private FinancingTargetService financingTargetService;
	@Autowired
	private RewardBidService rewardBidService;
	@Autowired
	private FriendInfoActivityDataQueryServiceImpl activityDataQueryService;

	@SuppressWarnings("rawtypes")
	@Override
	public void onApplicationEvent(FriendFirstBuyEvent event) {
		Map source = (Map) event.getSource();
		UserDO friendDO = (UserDO) source.get("friendDO");
		UserDO bidUserDO = (UserDO) source.get("bidUserDO");
		FinancingTargetBidDO bidDO = (FinancingTargetBidDO) source.get("bidDO");
		//新年邀请活动(new_year_invite.htm)——>送钱计划活动(send_money_plan.htm)
//		ActivityDO activity = activityService.get(ActivityDO.class, "20170110");
		ActivityDO activity = activityService.get(ActivityDO.class, "20170216");
		
		if (activity == null || activity.getBeginTime() == null || activity.getEndTime() == null) {
			return;
		}
		if (bidDO == null || !"CPB".equalsIgnoreCase(bidDO.getType())) {
			return;
		}
		if (bidUserDO == null || friendDO == null) {
			return;
		}
		long regTime = bidUserDO.getGmtCreate().getTime();
		if (regTime < activity.getBeginTime().getTime() || regTime > activity.getEndTime().getTime()) {
			return;
		}
		long payTime = bidDO.getPayTime().getTime();
		if (payTime < activity.getBeginTime().getTime() || payTime > activity.getEndTime().getTime()) {
			return;
		}

		BigDecimal exprBuyAmount = new BigDecimal(3650);
//		BigDecimal exprBuyAmount = new BigDecimal(14).multiply(new BigDecimal(36500)).divide(new BigDecimal(7)).divide(new BigDecimal(20), BigDecimal.ROUND_UP);
		FinancingTargetDO exprTarget = financingTargetService.find(FinancingTargetConstant.TAG_ACT_EXPR, exprBuyAmount);
		if(exprTarget == null) {
			return;
		}
		
		RewardBidService.BuyParam buyParam = new RewardBidService.BuyParam();
		buyParam.setBuyAmount(exprBuyAmount);
		buyParam.setTargetId(exprTarget.getId());
		buyParam.setUserId(friendDO.getId());
		
		LOG.info(String.format("邀请好友投资发放体验金,exprBuyAmount=%s,exprTarget.id=%s ,好友id为", exprBuyAmount, exprTarget.getId(),friendDO.getId()));
		rewardBidService.doBuy(buyParam);
		
		Map<String, String> map = new HashMap<String, String>();
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("fBeginCreateTime", sdf.format(activity.getBeginTime()));
		map.put("fEndCreateTime", sdf.format(activity.getEndTime()));
		map.put("fBeginBuyTime", sdf.format(activity.getBeginTime()));
		map.put("fEndBuyTime", sdf.format(activity.getEndTime()));
		map.put("fBidType", "CPB");
		List bidInviteUserList = (List)activityDataQueryService.query(friendDO.getId(), map);
		if(bidInviteUserList == null || bidInviteUserList.size() < 2) {
			return;
		}
		
		RegistryDO actExprRegistry = registryService.get(RegistryDO.class, "u." + friendDO.getId() + ".send-money-plan.expr");
		if(actExprRegistry != null) {
			return;
		}
		
		exprBuyAmount = new BigDecimal(20075);
//		exprBuyAmount = new BigDecimal(77).multiply(new BigDecimal(36500)).divide(new BigDecimal(7)).divide(new BigDecimal(20), BigDecimal.ROUND_UP);
		exprTarget = financingTargetService.find(FinancingTargetConstant.TAG_ACT_EXPR, exprBuyAmount);
		if(exprTarget == null) {
			return;
		}

		buyParam = new RewardBidService.BuyParam();
		buyParam.setBuyAmount(exprBuyAmount);
		buyParam.setTargetId(exprTarget.getId());
		buyParam.setUserId(friendDO.getId());
		
		LOG.info(String.format("邀请2位以上好友投资发放体验金,exprBuyAmount=%s,exprTarget.id=%s ,好友id为", exprBuyAmount, exprTarget.getId(),friendDO.getId()));
		if(rewardBidService.doBuy(buyParam)) {
			actExprRegistry = new RegistryDO();
			actExprRegistry.setId("u." + friendDO.getId() + ".send-money-plan.expr");
			actExprRegistry.setVal("20075");
			actExprRegistry.setVisible(true);
			registryService.create(actExprRegistry);
		}
	}

}
